package apps.softtech.fastmarket.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;
import apps.softtech.CustomView.SlideHolder;
import apps.softtech.Utility.ApplicationConstants;
import apps.softtech.fastmarket.R;
import apps.softtech.fastmarket.adapter.AdapterCategoryList.ViewHolder;
import apps.softtech.fastmarket.model.Category;

public class AdpaterCategoryListChild extends BaseAdapter{

	ArrayList<Category> mCategoriesListChild = new ArrayList<Category>();
	LayoutInflater inf;
	Context mContext;
	TabHost mTabHost;
	ViewPager mViewPager;
	SlideHolder mHolder;
	public AdpaterCategoryListChild(ArrayList<Category> mCategoriesList,Context mContext, TabHost mTabHost,ViewPager mViewPager
			,SlideHolder mHolder){
		this.mCategoriesListChild=mCategoriesList;
		this.mContext = mContext;
		inf = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mTabHost=mTabHost;
		this.mViewPager = mViewPager;
		this.mHolder = mHolder;
	}
	@Override
	public int getCount() {
		return mCategoriesListChild.size();
	}
	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View vy, ViewGroup arg2) {
		ViewHolder vh =null;
		if(vy==null){
			vh = new ViewHolder();
			vy = inf.inflate(R.layout.item_category_child, null);
			vh.tvTextContent = (TextView) vy.findViewById(R.id.tvItemCategoryChild);
			vh.ibNextPage = (Button) vy.findViewById(R.id.ibNextPageCategory);
			vh.layoutCategoryChild = (RelativeLayout) vy.findViewById(R.id.layoutCategoryChild);
			vy.setTag(vh);
		}else{
			vh = (ViewHolder) vy.getTag();
		}
		vh.tvTextContent.setText(""+mCategoriesListChild.get(position).getCategoryName());
		vh.ibNextPage.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
			}
		});
		final int idCategoryChild = position;
		vh.layoutCategoryChild.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
			int	idCategory = mCategoriesListChild.get(idCategoryChild).getCategoryId();
				sendBroadCastCatoryChildId(idCategory);//
				mTabHost.setCurrentTab(1);
				mViewPager.setCurrentItem(mTabHost.getCurrentTab());
				mHolder.close();
			}
		});
		return vy;
	}
	private void sendBroadCastCatoryChildId(int idCategory) {
		Intent inSendIdCategory = new Intent(ApplicationConstants.RECEIVER_CATEGORY_ID_CHILD);
		inSendIdCategory.putExtra("st_CategoryId", idCategory);
		mContext.sendBroadcast(inSendIdCategory);
	}
	class ViewHolder{
		TextView tvTextContent;
		Button ibNextPage;
		RelativeLayout layoutCategoryChild;
	}
}
